<?php
include('top.php');

$reel1 = rand( 0, 9);
$reel2 = rand( 0, 9);
$reel3 = rand( 0, 9);

$images = array("Cherries.jpg", "Lemon.jpg", "Melon.jpg", "Bar.jpg", "DoubleBar.jpg", "TripleBar.jpg", "Seven.jpg", "SevenBar.jpg", "Apple.jpg", "Strawberry.jpg");
?>
<h1>Welcome to Slots</h1>
<br /><br />
<center>
<script language="javascript" type="text/javascript">
<!-- Javascript Code area to run Game 
function Slots () 
{ 
  //Create variables
  var started = 0;
  var chips;
  var noOfTimesSpan = 0;
  
  //Create arrays for divs
  var containerA = new Array();
  var containerB = new Array();
  var containerC = new Array();
  
  //Create arrays to hold values for each reel
  var timeOutSpeed = new Array( "",10,10,10);
  var stopped = new Array( "",0,0,0);
  var slowreel = new Array( 1,0,0,0);
  var value = new Array( "", <?php echo $reel1." ,".$reel2." ,".$reel3; ?>);
  var spins = new Array( "",0,0,0);
  
  //Create array to hold images
  var images = new Array("Cherries.jpg", "Lemon.jpg", "Melon.jpg", "Bar.jpg", "DoubleBar.jpg", "TripleBar.jpg", "Seven.jpg", "SevenBar.jpg", "Apple.jpg", "Strawberry.jpg");
  
  //Create array to hold link to Interval for spin
  var reelInterval = new Array();
  
  //Alows methods (functions) to be called from outside class
  this.start = startgame;
  this.spinreel = spin;
  
  //Factory function to create XMLHttpRequest
  function createXMLHttpRequest() {
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
    try { return new XMLHttpRequest(); } catch(e) {}
    return null;
  }
  
  //Function to change the amount of chips in the database
  function updateChips(change)
  {
    //Get chips from database
	var request = createXMLHttpRequest();
	if (request == null)
	  return false;
	request.open("GET", "interface.php", false);
    request.send(null);
	if (request.responseText == "failed")
	  return false;
	else
	  chips = parseInt(request.responseText);
	//Change ammount of chips    
	chips += change;
	//Update chips in database
	request.open("GET", "interface.php?chips=" + chips + "&update=1", false);
    request.send(null);
	if (request.responseText == "failed")
	  return false;
	else
	  chips = parseInt(request.responseText);
	document.getElementById('chipsmsg').innerHTML = "<small>You have " + chips + " chips</small>";
	return true;
  }
  
  //Function to start game
  function startgame()
  {
    if (started != 1)
    {
	  //Remove chips used from chips
	  if (!updateChips(-10))
	  {
	    alert("An error occured: Either you are unable to play this game due to browser compatability issues or there is a database error."
		      + "\nPlease try again later. \nWe apologise for any inconvienance");
		return;
      }
	  //Start Game  
	  started = 1;
	  document.getElementById('message').innerHTML = "Spinning...";
	  
	  //Place divs in arrays
	  containerA[1] = document.getElementById("containerA1");
      containerB[1] = document.getElementById("containerB1");
      containerC[1] = document.getElementById("containerC1");
	  containerA[2] = document.getElementById("containerA2");
      containerB[2] = document.getElementById("containerB2");
      containerC[2] = document.getElementById("containerC2");
	  containerA[3] = document.getElementById("containerA3");
      containerB[3] = document.getElementById("containerB3");
      containerC[3] = document.getElementById("containerC3");
	  
	  //Randomise time spinning
	  for (index = 1; index < 4; index++)
	    spins[index] = Math.floor(Math.random()*10) + 6;
	  
	  //Set reels spinning
	  reelInterval[1] = setInterval("Game.spinreel(1)", 1);
	  reelInterval[2] = setInterval("Game.spinreel(2)", 1);
	  reelInterval[3] = setInterval("Game.spinreel(3)", 1);
    }//end if
  }//end startgame
  
  //Function to stop reels spinning and start slow reel spin
  function reelstop(reelNo)
  {
	//Stop spin and start slow spin
	clearInterval(reelInterval[reelNo]);
	slowreel[reelNo] = 1;
	noOfTimesSpan = 0;
	reelInterval[reelNo] = setInterval("Game.spinreel(" + reelNo + ")", timeOutSpeed[reelNo]);
  }
  
  //Function to change the interval of a slow reel
  function changeInterval(reelNo)
  {
    //Stop current slow spin
    clearInterval(reelInterval[reelNo]);
    if (timeOutSpeed[reelNo] < 50)
	{
	  //Half speed and set spinning
	  timeOutSpeed[reelNo] *= 2;
	  reelInterval[reelNo] = setInterval("Game.spinreel(" + reelNo + ")", timeOutSpeed[reelNo]);
	}//end if
	else 
	{
	  //Stop reel and end game if needed
	  stopped[reelNo] = 1;
	  if (reelNo == 3) 
	    endgame();
	}//end else
  }
  
  //Function to end game and reset
  function endgame()
  {
    if ((stopped[1] == 1) && (stopped[2] == 1) && (stopped[3] == 1))
	{
	  //Initilise addChips
	  var addChips = 0;
	  //Check if player has won anything
	  if ((value[1] == value[2]) && (value[2] == value[3]))
	    addChips = ((value[1] + 1) * 50);
	  else if (value[1] == value[2])
		addChips = (value[1] + 1) * 5;
	  else if (value[3] == value[2])
		addChips = (value[3] + 1) * 5;
	  else if (value[1] == value[3])
		addChips = (value[1] + 1) * 5;
	
	  //Display msg	
	  if (addChips != 0)
	    document.getElementById('message').innerHTML = "Congrats you won " + addChips + " chips";
      else
	    document.getElementById('message').innerHTML = "Click spin to try again";	
	  //Add winnings
	  if (!updateChips(addChips))
	    alert("An error occured: Unable to connect to the database. \nPlease try again later. \nWe apologise for any inconvienance");
		
	  
	  //Reset variables
      started = 0;
	  noOfTimesSpan = 0;
	  for (var index = 1; index <=3; index++)
	  {
	    stopped[index] = 1;
	    timeOutSpeed[index] = 10;
	    slowreel[index] = 0;
	  }//end for
	}//end if 
  }
  
  //Function to spin
  function spin(reelNo)
  {
    if (started == 1)
	{
	  //If bottom image is 0px then shift images through divs
	  if (containerC[reelNo].style.height == "0px")
      {
        containerC[reelNo].style.height = containerB[reelNo].style.height;
	    containerC[reelNo].style.backgroundImage = containerB[reelNo].style.backgroundImage;
        containerB[reelNo].style.height = containerA[reelNo].style.height;
        containerB[reelNo].style.backgroundImage = containerA[reelNo].style.backgroundImage;
        containerA[reelNo].style.height = "0px";
        containerA[reelNo].style.backgroundImage = "url('images/" + images[value[reelNo]] + "')";
		value[reelNo]++;
		if (value[reelNo] == 10)
		  value[reelNo] = 0;
      }
	
	  //Spin Reel
      containerC[reelNo].style.height = (parseInt(containerC[reelNo].style.height) - 5) + "px";

      if (parseInt(containerC[reelNo].style.height) <= 70) 
        containerA[reelNo].style.height = (parseInt(containerA[reelNo].style.height) + 5) + "px";
      else
        containerB[reelNo].style.height = (parseInt(containerB[reelNo].style.height) + 5) + "px";
		
	  //If middle image is center do checks to see if you stop or slow down	
	  if (containerA[reelNo].style.height == "35px")
	  {
	    if (slowreel[reelNo] == 0)
		{
          if (slowreel[(reelNo - 1)] == 1)
		  {
		    if (noOfTimesSpan == spins[reelNo])
			{
			  reelstop(reelNo);
			}//end if
			else
			{
			  noOfTimesSpan++;
		    }//end else
		  }//end if
		}//end if
		else 
		{
		  changeInterval(reelNo);
		}//end else
	  }//end if
    }//end if
  }//end spin
}//end Slots

var Game = new Slots();
 -->
</script>

<!-- The Game Area -->
<div class="slotMachineCase">
<div class="slotMachine" align="center">
<div id="message" class="buttoncase">Click Spin to start playing</div>
<div id="chipsmsg" class="buttoncase"><small>You have <?php echo $userChips; ?> chips</small></div>
<div class="slotsReel">
<?php 
$image = $reel1 - 1;
if ($image < 0) $image += 10;
?>
<div id="containerA1" class="slotsImageContainer" style="height:35px;background-image:url('images/<?php echo $images[$image]; ?>')"></div>
<?php 
$image -= 1;
if ($image < 0) $image += 10;
?>
<div id="containerB1" class="slotsImageContainer" style="height:80px;background-image:url('images/<?php echo $images[$image]; ?>')"></div>
<?php 
$image -= 1;
if ($image < 0) $image += 10;
?>
<div id="containerC1" class="slotsImageContainer" style="height:35px;background-image:url('images/<?php echo $images[$image]; ?>');background-position:top !important;"></div>
</div>
<div class="slotsReel">
<?php 
$image = $reel2 - 1;
if ($image < 0) $image += 10;
?>
<div id="containerA2" class="slotsImageContainer" style="height:35px;background-image:url('images/<?php echo $images[$image]; ?>')"></div>
<?php 
$image -= 1;
if ($image < 0) $image += 10;
?>
<div id="containerB2" class="slotsImageContainer" style="height:80px;background-image:url('images/<?php echo $images[$image]; ?>')"></div>
<?php 
$image -= 1;
if ($image < 0) $image += 10;
?>
<div id="containerC2" class="slotsImageContainer" style="height:35px;background-image:url('images/<?php echo $images[$image]; ?>');background-position:top !important;"></div>
</div>
<div class="slotsReel">
<?php 
$image = $reel3 - 1;
if ($image < 0) $image += 10;
?>
<div id="containerA3" class="slotsImageContainer" style="height:35px;background-image:url('images/<?php echo $images[$image]; ?>')"></div>
<?php 
$image -= 1;
if ($image < 0) $image += 10;
?>
<div id="containerB3" class="slotsImageContainer" style="height:80px;background-image:url('images/<?php echo $images[$image]; ?>')"></div>
<?php 
$image -= 1;
if ($image < 0) $image += 10;
?>
<div id="containerC3" class="slotsImageContainer" style="height:35px;background-image:url('images/<?php echo $images[$image]; ?>');background-position:top !important;"></div>
</div>
<div class="buttoncase">
<div id="spin" class="DivButton" style="width:170px;" onclick="Game.start()">Spin</div>
<small>Each spin costs 10 chips.</small>
</div>
</div></div>
<!-- End of Game Area -->
</center>
<?php 
include('bottom.php');
?>